package com.lfg.leetcode.leetcode0;

public class No29Divide {

    public static int divide(int dividend, int divisor) {
        if (dividend == -2147483648 && divisor == -1) return 2147483647;//正整数和负整数最大值差1
        if (dividend == 0) {
            return 0;
        }
        if (divisor == 1) {
            return dividend;
        }
        Long i = 0l;
        boolean flag = dividend > 0 ^ divisor > 0;
        long dvd = Math.abs(Long.valueOf(dividend));
        long dvs = Math.abs(Long.valueOf(divisor));

        while (dvd >= dvs) {
            long dvs2 = dvs;
            long i2 = 1;
            while (true) {
                dvs2 = dvs2 << 1;
                if (dvd < dvs2) {
                    dvs2 = dvs2 >> 1;
                    break;
                }
                i2 = i2 << 1;
            }
            dvd = dvd - dvs2;
            i = i + i2;
        }

        if (flag) {
            return -i.intValue();
        }
        return i.intValue();
    }

    public static void main(String[] args) {
        int l = divide(-2147483648, 2);
        System.out.println(l);
//        System.out.println("aaaaaaaaaxxxxxxxx".indexOf("xxx"));
    }
}

